package com.lsl.test1;

/**
 * 两个线程间隔打印1到100
 *
 * @author lishuailong
 * @create 2020-07-06 14:17
 */
public class 线程通信练习 {
    public static void main(String[] args) {

        Runnable runnable = new Runnable() {
            private int anInt = 0;

            @Override
            public void run() {
                while (true) {
                    synchronized (this) {
                        this.notify();
                        if (anInt < 100) {
                            anInt++;
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + ":" + anInt);
                        } else {
                            break;
                        }
                        try {
                            this.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };

        Thread thread = new Thread(runnable);
        Thread thread1 = new Thread(runnable);

        thread.setName("甲");
        thread1.setName("乙");

        thread.start();
        thread1.start();

    }

}

